package repository

import (
	"pq/models"
)

type UserRepository struct{

}
type UserInfo struct{
	Name        string `json:"name"`
	Nickname    string `json:"nickname"`
	Category    string `json:"test_category_title"`
	Category_id int    `json:"test_category_id"`
	AreaCode    string `json:"area_code"`
}

type UserUnSafeInfo struct {
	Id int `json:"id"`
	AreaCode string `json:"area_code"`
}
func (ur UserRepository) SaveUser(user models.PqUser)(int64,error){
	master := models.GetMasterDb()
	//return master.InsertOrUpdate(user,"id")
	return master.Update(&user)
}
func (ur UserRepository) GetUser(id int) (UserInfo,error){
	o := models.GetSlaveDb()

	var user UserInfo
	_ = o.Raw(`select u.name as name,u.nickname as nickname,u.pq_test_category_id as category_id,c.title as category,u.area_code 
			from pq_user u
			left join pq_test_category c on c.id=u.pq_test_category_id
			where u.id=?`, id).QueryRow(&user)
	return user,nil
}

func (ur UserRepository) GetUserByPhone(phone string)(UserUnSafeInfo,error) {
	o := models.GetSlaveDb()

	var user UserUnSafeInfo
	_ = o.Raw(`select u.id,u.area_code 
			from pq_user u
			where u.name=?`,phone).QueryRow(&user)
	return user,nil
}
